#!/bin/bash

# This script generates a .deb package for the peer module of OurGrid(ourgrid.org)
# It was based on rules provided by debian in http://www.debian.org/doc/maint-guide/

PEER_FOLDER=./

#Defining some variables
PACKAGE_NAME=Peer
TEMP_FOLDER=`mktemp -d -t Ourgrid-XXXXXX`
PACKAGE_TEMP_FOLDER=$TEMP_FOLDER/$PACKAGE_NAME
DEBIAN_FOLDER=$PACKAGE_TEMP_FOLDER/DEBIAN
BIN_FOLDER=$PACKAGE_TEMP_FOLDER/usr/bin
SHARE_FOLDER=$PACKAGE_TEMP_FOLDER/usr/share/ourgrid-peer
CONF_FOLDER=$PACKAGE_TEMP_FOLDER/etc/ourgrid-peer
VERSION=1.0
CONTROL_FILE=""
CHANGELOG_FILE=""

# Functions

#Prints the help
usage()
{
	echo "Usage: ./peer_package_generator.sh [OPTION]
Creates the Peer package.

Mandatory arguments to long options are mandatory for short options too.
  -cf, --control     tells the control file location to the script
  -cl, --changelog   tells the changelog file location to the script
  -v,  --version     tells the the version of the package
  -h,  --help        display this help and exit

Exit status:
 0  if OK,
 1  for any problem"
}

#This part generates the directory's structure
generate_debian_structure()
{
	echo "Generating debian structure in '$PACKAGE_TEMP_FOLDER'"
	rm -rf $PACKAGE_TEMP_FOLDER
	mkdir -p $DEBIAN_FOLDER
	mkdir -p $BIN_FOLDER
	mkdir -p $LIB_FOLDER
	mkdir -p $CONF_FOLDER
	mkdir -p $LOG_FOLDER
}

#Copy peer files to package respective folders
copy_files()
{
	echo "Copying files from '$PEER_FOLDER' to respective package directories"
	cp $PEER_FOLDER/peer $BIN_FOLDER
	#cp $PEER_FOLDER/resources/* $SHARE_FOLDER
	cp $PEER_FOLDER/peer.properties $CONF_FOLDER
	cp $PEER_FOLDER/example.sdf $CONF_FOLDER
	cp $PEER_FOLDER/COPYING $CONF_FOLDER/COPYING
	cp $PEER_FOLDER/COPYING $DEBIAN_FOLDER/copyright
}

#create the init script
create_init_script()
{
	echo "Creating the control file in respective package directory"
	echo "#! /bin/sh
### BEGIN INIT INFO
# Provides:          peer
# Required-Start:    \$remote_fs \$syslog
# Required-Stop:     \$remote_fs \$syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the peer module of OurGrid(ourgrid.org)
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=\"Starts the peer module of OurGrid(ourgrid.org)\"
NAME=peer
DAEMON=/usr/bin/\$NAME
DAEMON_ARGS=\"start\"
PIDFILE=/var/run/\$NAME.pid
SCRIPTNAME=/etc/init.d/\$NAME

# Exit if the package is not installed
[ -x \"\$DAEMON\" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/\$NAME ] && . /etc/default/\$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --quiet --pidfile \$PIDFILE --exec \$DAEMON --test > /dev/null \\
		|| return 1
	start-stop-daemon --start --quiet --pidfile \$PIDFILE --exec \$DAEMON -- \\
		\$DAEMON_ARGS \\
		|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile \$PIDFILE --name \$NAME
	RETVAL=\"\$?\"
	[ \"\$RETVAL\" = 2 ] && return 2

	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec \$DAEMON
	[ \"\$?\" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	rm -f \$PIDFILE
	return \"\$RETVAL\"
}

case \"\$1\" in
  start)
	[ \"\$VERBOSE\" != no ] && log_daemon_msg \"Starting \$DESC\" \"\$NAME\"
	do_start
	case \"\$?\" in
		0|1) [ \"\$VERBOSE\" != no ] && log_end_msg 0 ;;
		2) [ \"\$VERBOSE\" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ \"\$VERBOSE\" != no ] && log_daemon_msg \"Stopping \$DESC\" \"\$NAME\"
	do_stop
	case \"\$?\" in
		0|1) [ \"\$VERBOSE\" != no ] && log_end_msg 0 ;;
		2) [ \"\$VERBOSE\" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
       status_of_proc \"\$DAEMON\" \"\$NAME\" && exit 0 || exit \$?
       ;;
 #reload|force-reload)
  restart|force-reload)
	log_daemon_msg \"Restarting \$DESC\" \"\$NAME\"
	do_stop
	case \"\$?\" in
	  0|1)
		do_start
		case \"\$?\" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo \"Usage: \$SCRIPTNAME {start|stop|status|restart|force-reload}\" >&2
	exit 3
	;;
esac

:" > $INIT_FOLDER/ourgrid-peer
}

#Create the control file
generate_control_file()
{
	
	if [ ! -z $CONTROL_FILE ]
	then
		echo "Copying the control file from '$CONTROL_FILE' to respective package directory"
		cp $CONTROL_FILE $DEBIAN_FOLDER/control
	else
		echo "Creating the control file in respective package directory"
		echo "Package: ourgrid-peer
Version: $VERSION
Section: unknown
Priority: optional
Architecture: all
Depends: sun-java6-jre, ourgrid-common 
Maintainer: OurGrid.org
Homepage: ourgrid.org
Description: This package installs the Ourgrid's module Peer." > $DEBIAN_FOLDER/control
	fi
}

#create the changelog file
generate_changelog_file()
{
	if [ ! -z $CHANGELOG_FILE ]
	then
		echo "Copying the changelog file from '$CHANGELOG_FILE' to respective package directory"
		cp $CHANGELOG_FILE $DEBIAN_FOLDER/changelog
	else
		echo "Creating the changelog file in respective package directory"
		echo "Current release $VERSION" > $DEBIAN_FOLDER/changelog
	fi
}

#create the rules file
generate_rules_file()
{
	echo " " > $DEBIAN_FOLDER/rules
	chmod +x $DEBIAN_FOLDER/rules
}

#Create extra scripts
generate_extra_scripts()
{
	echo "Creating extra scripts"
}

#MAIN
# Loop until all parameters are used up
while [ "$1" != "" ]; do
     case $1 in
        -v | --version )        shift
                                VERSION=$1
                                ;;
        -cf | --control )       shift
                                CONTROL_FILE=$1
                                ;;
        -cl | --changelog )     shift
                                CHANGELOG_FILE=$1
                                ;;
        -h | --help )           usage
                                exit 0
                                ;;
    esac
    # Shift all the parameters down by one
    shift
done

echo "Generating debian file to peer module of OurGrid(ourgrid.org). The source files are in $PEER_FOLDER."
generate_debian_structure
copy_files
create_init_script
generate_control_file
generate_changelog_file
#generate_rules_file
generate_extra_scripts

#Generate the package
echo "Generating the package into `pwd`"
dpkg --build $PACKAGE_TEMP_FOLDER .

#Remove the temp files
echo "Removing Temp files"
rm -rf $TEMP_FOLDER

exit 0

